package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import br3akr.DeadlyDinnerParty.Server.Engine.Session;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;
import br3akr.DeadlyDinnerParty.Server.World.Area;
import br3akr.DeadlyDinnerParty.Server.World.GameCharacter;
import br3akr.DeadlyDinnerParty.Server.World.Item;

public class GiveAction extends Action {
	boolean actionComplete;
    private ActionResponse actionResponse;
    public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		tempKeywords.add("Give");
		tempKeywords.add("Gi");
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public GiveAction() {
        super("Handcuff", false);
    }
    
    public ActionResponse doAction(String action, Session session) throws DeadlyDinnerPartyException {
		if (action == null) {
			throw new DeadlyDinnerPartyException ("No action found.");
		}
		if (session == null) {
			throw new DeadlyDinnerPartyException ("No session found.");
		}
		
    	StringTokenizer tokenizedAction = new StringTokenizer(action);
    	String keyword = tokenizedAction.nextToken();
    	String param1 = null;
    	String param2 = null;
    	
    	if (tokenizedAction.hasMoreTokens()) {
    		param1 = tokenizedAction.nextToken();
    	}
    	
    	if (param1 == null) {
    		throw new DeadlyDinnerPartyException ("Who do you want to give something to?");
    	}
    	
    	param2 = action.substring(action.indexOf(param1) + param1.length()).trim();
    	
		GameCharacter actor = session.getCharacter();
		
		if (actor == null) {
			throw new DeadlyDinnerPartyException ("No character found for your session.");
		}
    	
    	actionResponse = new ActionResponse();
    	actionResponse.setActingCharacter(session.getCharacter());
    	Area location = session.getCharacter().getLocation();
    	
    	Set<GameCharacter> localOnlookers = location.getAreaOnlookers(session.getCharacter(), null);
    	actionResponse.addAffectedArea(location);
    	GameCharacter target = null;
    	Item item = null;
    	
		switch (keyword.toLowerCase()) {
			case "give":
			case "gi":		    	
		    	target = location.getCharacter(param1);
		    	
		    	if (target == null || target.isHiddenFrom(actor)) {
		    		throw new DeadlyDinnerPartyException ("There is no one here by that name.");
		    	}
		    	
		    	item = actor.getInventoryItem(param2);
		    	
		    	if (item == null) {
		    		throw new DeadlyDinnerPartyException ("You aren't carrying anything like that.");
		    	}
				
				actor.removeItem(item);
				target.addItem(item);
				
				actionResponse.addOnlookers(localOnlookers, actor.getName() + " gives something to " + target.getName() + ".");
				actionResponse.setActingCharacterDescription("You give " + target.getName() + " " + item.getName() + ".");
				actionResponse.addTargetCharacter(target);
				actionResponse.setTargetCharacterDescription(target, actor.getName() + " gives you " + item.getName() + ".");
				beginRecovery(session.getCharacter(), 3000);
				
				break;
			default:
				break;
		}
        
    	actionComplete = true;
        return actionResponse;
    }
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }
}